﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Threading;

namespace WEI_Share
{
	/// <summary>
	/// Interaction logic for WinSatProgress.xaml
	/// </summary>
	public partial class WinSatProgress : Window
	{
		DispatcherTimer processCheckerAndUpdater = new DispatcherTimer(); 
		int timeBeforeCheck = 5;

		public WinSatProgress()
		{
			InitializeComponent();

			uxMain.ContentRendered += new EventHandler(uxMain_ContentRendered);
			processCheckerAndUpdater.Tick += new EventHandler(processCheckerAndUpdater_Tick);
			processCheckerAndUpdater.Interval = new TimeSpan(0, 0, 1);
		}

		


		#region Event Handlers
		/// <summary>
		/// Handles the timer interval events
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void processCheckerAndUpdater_Tick(object sender, EventArgs e)
		{
			if (Helpers.Utilities.WinsatExecutableExited)
			{
				SetupReturnValueAndCloseWindow(true);
			}
			else if ((timeBeforeCheck--) > 0)
			{
				SetupMessage(false);
			}
			else
			{
				processCheckerAndUpdater.Stop();
				if (CheckStatusOfExecutable())
					processCheckerAndUpdater.Start();
			}
		}


		/// <summary>
		/// Handles the main event equivalent to winfows form .shown
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void uxMain_ContentRendered(object sender, EventArgs e)
		{
			if (Helpers.Utilities.RunWinSatProgram())			
			{
				SetupMessage(false);

				processCheckerAndUpdater.Start();
			}
			else
			{
				//did not launch
				uxMessage.Text = "Unable to launch Windows Site Assessment.  Click the Close button to continue.";
				uxClose.Visibility = Visibility.Visible;
			}
		}
		#endregion


		#region SetupMessage(...)
		/// <summary>
		/// Handles the message dispaly for the control
		/// </summary>
		/// <param name="isCheckingProcess"></param>
		void SetupMessage(bool isCheckingProcess)
		{
			//setup message
			uxMessage.Text =
				(
					isCheckingProcess ?
								"Attempting to verify if WinSAT is running."
								:
								"WinSAT has been launched.  WEI Share will attempt to verify if it is still running in " + timeBeforeCheck.ToString() + " seconds."
				) +
								Environment.NewLine + Environment.NewLine + Environment.NewLine +
								"If it is unable to verify if it is still running, this window will close and you will need to click " +
								"the \"Rate My System now\" button again once it finishes.";
		}
		#endregion


		/// <summary>
		/// Checks the status of the executable
		/// </summary>
		bool CheckStatusOfExecutable()
		{
			bool startTimer = false;

			SetupMessage(true);

			bool? stillRunning = Helpers.WMISystemInfo.IsProcessStillRunning("winsat.exe");
			if (stillRunning.HasValue)
			{
				if (stillRunning.Value)
				{
					timeBeforeCheck = 5;

					SetupMessage(false);

					startTimer = true;
				}
				else
				{
					SetupReturnValueAndCloseWindow(true);
				}
			}
			else
			{
				uxMessage.Text = "Unable to verify if the Windows Site Assessment application is still running." +
								Environment.NewLine + Environment.NewLine + 
								"If it appears to still be running, wait till it completes and then click the Close button to continue." +
								Environment.NewLine + Environment.NewLine + 
								"Otherwise, click the Close button to continue.";
				uxClose.Visibility = Visibility.Visible;
			}

			return startTimer;
		}

		/// <summary>
		/// Used to centralize the return value in code
		/// </summary>
		/// <param name="returnValue"></param>
		void SetupReturnValueAndCloseWindow(bool? returnValue)
		{
			if (processCheckerAndUpdater != null)
				processCheckerAndUpdater.Stop();

			this.DialogResult = returnValue;
			this.Close();
		}

	}
}
